package chapter5.predestroy_annotation;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Service;

/**
 * 
 * @author chauthang
 * @comment example add method into constructor when create bean and call method
 *          destroy when destroy bean
 */
@Service
public class DestructiveBeanWithJSR250 {
	@Value("Thanh Thang")
	private String name;

	@PostConstruct
	public void init() {
		System.out.println("Ininializing Bean");
		if (name == null) {
			System.out.println("Name will using default");
			name = "Chau Thang";
		} else {
			System.out.println("Name : " + name);
		}
	}

	@PreDestroy
	public void destroy() {
		System.out.println("Destroy Bean");
		if (name != null) {
			System.out.println("Bean will reset null");
			name = null;
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static void main(String[] args) {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:chapter5/predestroy_annotation/applicationContext.xml");
		ctx.refresh();

		ctx.getBean("destructiveBeanWithJSR250");
		ctx.destroy();
	}
}
